package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.picture.PictureAlbumService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureAlbum;
import com.qdxwx.models.picture.PictureQuery;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ajax/picture")
public class AjaxPictureController extends BaseController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PictureAlbumService pictureAlbumService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/get.json")
    @ResponseBody
    public AjaxResult view(HttpServletRequest request){
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return AjaxResult.error("参数异常");
        Picture picture = pictureService.get(id);
        if(picture == null) return AjaxResult.error("找不到指定素材");
        return AjaxResult.success(picture);
    }

    @GetMapping("/list.json")
    @ResponseBody
    public AjaxResult list(HttpServletRequest request){
        Integer channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Integer categoryId = TKConvert.toInteger(request.getParameter("categoryId"),null);
        Integer subjectId = TKConvert.toInteger(request.getParameter("subjectId"),null);
        Integer elite = TKConvert.toInteger(request.getParameter("elite"),null);
        Integer isTop = TKConvert.toInteger(request.getParameter("isTop"),null);
        Integer levelId = TKConvert.toInteger(request.getParameter("levelId"),null);
        Integer isFree = TKConvert.toInteger(request.getParameter("isFree"),null);
        Integer orderBy = TKConvert.toInteger(request.getParameter("orderBy"));
        Long userId = TKConvert.toLong(request.getParameter("userId"));
        Integer page = TKConvert.toInteger(request.getParameter("page"),1);
        int limit = TKConvert.toInteger(request.getParameter("limit"),10);
        PictureQuery query = new PictureQuery();
        query.setChannelId(channelId);
        query.setIsTop(isTop);
        query.setIsElite(elite);
        query.setSubjectId(subjectId);
        query.setCategoryId(categoryId);
        query.setOrderBy(orderBy);
        query.setStatus(1);
        query.setUserId(userId);
        query.setLevelId(levelId);
        query.setIsFree(isFree);
        if (categoryId > 0) {
            query.setCategoryIds(categoryService.getChildIds(categoryId));
        }
        PageRequest pageRequest = new PageRequest();
        pageRequest.setCurrentPage(page);
        pageRequest.setPageSize(limit);
        List<Picture> pictureList = pictureService.getList(query, pageRequest);
        return AjaxResult.success(pictureList);
    }

    /**
     * 素材轮播
     */
    @GetMapping("/view/slides.json")
    @ResponseBody
    public Map<String, Object> slides(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        Picture picture = pictureService.get(id);
        if (picture == null) return AjaxResult.error("找不到指定素材");
        PictureAlbum pictureAlbum = pictureAlbumService.getByPictureId(picture.getId());
        List<Picture> pictureList = new ArrayList<>();
        if (pictureAlbum != null) {
            pictureList.add(picture);
            pictureList.addAll(pictureService.getListByAlbumId(pictureAlbum.getId(), null));
        } else {
            pictureList.add(picture);
        }
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (Picture item : pictureList) {
            Map<String, Object> datas = new HashMap<>();
            datas.put("alt", item.getTitle());
            datas.put("pid", item.getId());
            datas.put("src", item.getViewUrl());
            datas.put("thumb", item.getCoverUrl());
            dataList.add(datas);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("title", pictureAlbum != null ? pictureAlbum.getTitle() : picture.getTitle());
        map.put("id", pictureAlbum != null ? pictureAlbum.getId() : picture.getId());
        map.put("start", 0);
        map.put("data", dataList);
        return map;
    }
}
